<?php

namespace app\admin\controller;

use app\admin\extend\Response;
use app\admin\extend\View;
use app\admin\logic\system\LoginLogic;
use app\admin\validate\LoginValidate;
use app\Request;
use think\exception\ValidateException;

class LoginController extends \app\BaseController {

    use View;

    // 登录
    public function index(Request $request) {
        if ($post = $request->post()) {
            try {
                $this->validate($post, LoginValidate::class);
            } catch (ValidateException $e) {
                return json(Response::setError($e->getMessage()));
            }
            
            $token = LoginLogic::loginReturnToken($post['username'], $post['password']);
            if (!$token) {
                return json(Response::setError('登录失败'));
            }

            $response = json(Response::setSuccess(url('index/index')->build()));
            $response->cookie(LoginLogic::TOKEN_ADMIN, $token, 7*24*3600);
            return $response;
        }

        // 判断是否可以自动登录
        if($token = LoginLogic::loginByToken($request)) {
            $response = redirect(url('index/index')->build());
            $response->cookie(LoginLogic::TOKEN_ADMIN, $token, 7*24*3600);
            return $response;
        }

        // 否则,显示账号密码
        return $this->fetch();
    }

    // 登出
    public function logout(Request $request) {
        LoginLogic::logout();
        return redirect(url('login/index'));
    }

    //获取头像验证码
    public function getyzm() {
        //1.创建黑色画布
        $image = imagecreatetruecolor(100, 30);

        //2.为画布定义(背景)颜色
        $bgcolor = imagecolorallocate($image, 255, 255, 255);

        //3.填充颜色
        imagefill($image, 0, 0, $bgcolor);

        // 4.设置验证码内容
        //4.1 定义验证码的内容
        $content = "ABCEFHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxy13456789";
        //4.1 创建一个变量存储产生的验证码数据，便于用户提交核对
        $captcha = "";
        for ($i = 0; $i < 4; $i++) {
            // 字体大小
            $fontsize = 12;
            // 字体颜色
            $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            // 设置字体内容
            $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
            $captcha .= $fontcontent;
            // 显示的坐标
            $x = ($i * 100 / 4) + mt_rand(5, 10);
            $y = mt_rand(5, 10);
            // 填充内容到画布中
            imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
        }

        LoginLogic::saveYzm($captcha);

        //4.3 设置背景干扰元素
        for ($$i = 0; $i < 100; $i++) {
            $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
        }

        //4.4 设置干扰线
        for ($i = 0; $i < 2; $i++) {
            $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
        }
        //5.向浏览器输出图片头信息
        header('content-type:image/png');
        //6.输出图片到浏览器
        imagepng($image);
        //7.销毁图片
        imagedestroy($image);
    }

}
